package us.icitap.commandcenter.business;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.utils.Utils;
import us.icitap.utils.AppPdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.vaadin.server.StreamResource.StreamSource;

public class PdfCallDocument extends AppPdf implements StreamSource{

	
	private Call call;
	
	public PdfCallDocument(Call call,String printingUser, String fileName) {
		super(printingUser,fileName);
		this.call = call;
	}
	
	public void createDocument(Call call) throws DocumentException, IOException{
		Document d = this.getDocument();
		
		PdfPTable table = new PdfPTable(2);
		PdfPCell cell;
		
		cell = new PdfPCell(new Phrase("Data/Ora e telefonates"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(Utils.formatedDateTime(call.getRecordDate())));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("TELEFONUESI"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.isAnonymousCaller()?"ANONIM":call.getCaller()));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("ARSYEJA E TELEFONATES"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getCallReason()!=null?call.getCallReason().getTag():""));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("KLASIFIKIMI I TEL."));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getActualGrade()!=null?call.getActualGrade().getTag():""));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("ZONA E MBULIMIT"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getMainDispatcher()!=null?call.getMainDispatcher().getName():""));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("PERSHKRIMI"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getDescription()));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("ADRESA"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getAddress()));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("NJOFTUAR:"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getNotificationsInfo()));
		table.addCell(cell);
		
		cell = new PdfPCell(new Phrase("PARTULLAT E DERGUARA"));
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(call.getPatrolsInfo()));
		table.addCell(cell);
		
		if(call.getActualStatus()==CallStatusEnum.V){
			cell = new PdfPCell(new Phrase("DATA/ORA E VERIFIKIMIT"));
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(Utils.formatedDateTime(call.getVerificationDate())));
			table.addCell(cell);
			
			cell = new PdfPCell(new Phrase("ASRYEJA PAS VERIFIKIMIT"));
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(call.getVerifiedCallReason()!=null?call.getVerifiedCallReason().getTag():""));
			table.addCell(cell);
			
			cell = new PdfPCell(new Phrase("SHENIME MBI VERIFIKIMIN"));
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(call.getVerificationNotes()));
			table.addCell(cell);
			
			cell = new PdfPCell(new Phrase("MASAT E MARRA"));
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(call.getPoliceActionListString()));
			table.addCell(cell);
		}
		else if(call.getActualStatus() == CallStatusEnum.N)
		{
			cell = new PdfPCell(new Phrase("AKOMA NUK KA NISUR VERIFIKIMI I TELEFONATES"));
			cell.setColspan(2);
			table.addCell(cell);
		}
		else if(call.getActualStatus() == CallStatusEnum.WV)
		{
			cell = new PdfPCell(new Phrase("TELEFONATA ESHTE NE PROCES VERIFIKIMI"));
			cell.setColspan(2);
			table.addCell(cell);
		}
		
		d.add(table);
		d.close();
	}
	
	
	@Override
	public InputStream getStream() {
		try {
			this.createDocument(call);
			return this.getInputStream();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		
		
	}

}
